//
//  main.m
//  Injector
//
//  Created by ∞ on 13/11/07.
//  Copyright Emanuele Vulcano 2007. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <stdio.h>
#import <string.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char *argv[]) {
	if (getuid() == 0 || geteuid() == 0) {
		L0Printf("Executing as root: uid = %d, gid = %d; euid = %d, egid = %d\n",
			  getuid(), getgid(), geteuid(), getegid());
		
		L0Printf("argc = %d\n", argc);
		int i;
		for (i = 0; i < argc; i++)
			L0Printf("argv[%d] = %s\n", i, argv[i]);

		if (argc != 2 || strcmp(argv[1], "--PlSAuthorize") != 0)
			return 1; // errors out.
		
		int result = 0;
		NSAutoreleasePool* pool = [NSAutoreleasePool new];
		NSBundle* bundle = [NSBundle mainBundle];
		NSString* exec = [bundle executablePath];
		exec = [[exec stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"PlugSuit Agent"];
		const char* execC = [exec fileSystemRepresentation];
		L0Log("Now changing permissions on %s.\n", execC);
				
		if (chmod(execC, 02755) == -1)
			result = 1;
		
		if (result == 0 && chown(execC, -1, 9) == -1)
			result = 1;

		[pool release];
		fprintf(stdout, "K");
		return result;
	} else {
		L0PrintfS("Root only, thank you. Try running the Agent instead through the preference pane.");
		return 1;
	}
}
